//
//  YMSearchView.swift
//  YMSwiftUI
//
//  Created by anscen on 2022/4/1.
//

import SwiftUI

private let queue = BaseSearchQueue()
struct YMSearchView: View {
    @State private var listData: [String] = []
    @State private var nickName :String = ""
    var body: some View {
        VStack(alignment: .leading, spacing: 10) {
            YMSearchTextView(textName: $nickName)
            List {
                ForEach.init(listData,id:\.self) {objc in
                    YMSearchCell(title: objc)
                }.onDelete { sets in
                    if let row = sets.first{
                        queue.deleteData(keyword: self.listData[row]) { success in
                            if success{
                                self.listData.remove(at: row)
                            }
                        }
                    }
                }
            }.onAppear {
                self.loadData()
            }.listStyle(.plain).padding(EdgeInsets.zero).navigationBarHidden(true)
        }
    }
    func loadData(){
        queue.searchData { datas in
            self.listData = datas
        }
    }
}
struct YMSearchCell :View{
    @State var title :String
    @State private var isActive = false
    var body : some View{
        NavigationLink(isActive: $isActive) {
            YMSearchResultView(nickName: title)
        } label: {
            VStack {
                HStack(alignment: .center, spacing: 10) {
                    Image("icon_home_his").frame(width: 20, height: 20, alignment: .center)
                    Text(title).font(Font.system(size: 14)).foregroundColor(Color.appx333333())
                    Spacer()
                }
            }.padding(EdgeInsets(top: 12, leading: 0, bottom: 12, trailing: 0))//.listRowSeparator(.hidden)
        }.onChange(of: isActive) { value in
            if value{
                queue.insertData(keyword: title) { success in
                    
                }
            }
        }
    }
}
struct YMSearchTextView :View{
    @Environment(\.presentationMode) var presentationMode
    @Binding var textName :String
    @State private var isActiva = false
    var body: some View{
        VStack {
            HStack(alignment: .center, spacing: 10) {
                Button {
                    self.presentationMode.wrappedValue.dismiss()
                } label: {
                    HStack {
                        Image("icon_nav_back").frame(width: 44, height: 44, alignment: .center)
                    }
                }
                ZStack(alignment: .center) {
                    TextField("请输入小说名称或者作者名", text: $textName).textFieldStyle(.plain).padding(EdgeInsets.init(top: 7, leading: 10, bottom: 7, trailing: 10))
                }.overlay(RoundedRectangle(cornerRadius: 5, style: .continuous).stroke(AppXDDDDDD, lineWidth: 1))
                NavigationLink(isActive: $isActiva) {
                    YMSearchResultView(nickName: textName)
                } label: {
                    Text("搜索").frame(width: 65, height: 36, alignment: .center).foregroundColor(Color.appxffffff())
                }.background(AppColor).cornerRadius(5).onChange(of: isActiva) { value in
                    if value{
                        debugPrint(textName)
                        queue.insertData(keyword: textName) { success in
                            
                        }
                    }
                }
            }.padding([.trailing])
        }.background(Color.appxffffff())
    }
}
//struct YMSearchView_Previews: PreviewProvider {
//    static var previews: some View {
//        YMSearchCell(title: "123456")
//    }
//}

